package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author fvkli
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        //1.查询护理计划
        NursingPlan nursingPlan = this.getById(id);
        //2.查询中间表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //3.数据封装
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> list = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        vo.setProjectPlans(list);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.nursingPlanMapper.insert(nursingPlan);
        dto.getProjectPlans().forEach(item -> {
            item.setPlanId(nursingPlan.getId());
        });
        boolean b = nursingProjectPlanService.saveBatch(dto.getProjectPlans());
        return b ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        //修改
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);

        //删除
        LambdaQueryWrapper<NursingProjectPlan> eq = Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId());
        nursingProjectPlanService.remove(eq);
        //绑定
        List<NursingProjectPlan> planList = dto.getProjectPlans();

        if(!CollectionUtils.isEmpty(planList)){
            for (NursingProjectPlan projectPlan : planList) {
                projectPlan.setPlanId(nursingPlan.getId());
            }

            boolean b = nursingProjectPlanService.saveBatch(planList);
            return b ? 1 : 0;
        }


        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids) {
        return this.removeByIds(Arrays.asList(ids)) ? 1 : 2;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {
        //nursingPlanMapper.deleteNursingPlanById(id);
        this.removeById(id);
/*        LambdaQueryWrapper<NursingProjectPlan> eq = Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(eq);
        boolean b=true;
        for (NursingProjectPlan projectPlan : projectPlans) {
            b = nursingProjectPlanService.removeById(projectPlan.getId());
        }*/
        boolean removed = nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        return removed?1:2;
    }

    @Override
    public List<NursingPlan> findAll() {
            //先查询缓存
        List<NursingPlan> nursingPlans = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        //判断缓存是否为空
        if (!CollectionUtils.isEmpty(nursingPlans)) {
            return nursingPlans;
        }
        List<NursingPlan> list = this.list();
        return list;
    }
}
